/*
 * @Author: 王鹏 2911783749@qq.com
 * @Date: 2022-11-07 18:35:11
 * @LastEditors: 王鹏 2911783749@qq.com
 * @LastEditTime: 2022-11-08 09:40:39
 * @FilePath: \vue3Mixiu\code\src\untils\http.ts
 * @Description: 封装axios
 */
import axios from 'axios';
import router from '../router';
// 请求拦截
axios.interceptors.request.use((config:any) => {
    // 携带token
    if(localStorage.token){
        config.headers.Authorization = localStorage.token
    }
    return config;
},(error) => {
    return Promise.reject(error)
})

// 响应拦截
axios.interceptors.response.use(response => {
    return response;
},(error) => {
    const {status} = error.response

    if(status === 401){
        localStorage.removeItem("token")
        router.push("/login")
    }
    console.log(error);
    // @ts-ignore
    ElMessage({
        message:error.response.data.msg,
        type:'error'
    }) 
    return Promise.reject(error)
})

export default axios;